package wrapper_package;

public class Interger_wrapper 
{
	public static void main(String[] args) 
	{
		// constructor that takes int primitive as argument.
		Integer i = new Integer(23);
		System.out.println("i: " + i);
		
		// constructor that takes string representation int primitive as argument.
		Integer i2 = new Integer("12");
		System.out.println("i2: " + i2);
		
		// If unable to parse int from string throws NumberFormatException
		try
		{
			Integer i3 = new Integer("two");
			System.out.println("i3: " + i3);
		}
		catch(NumberFormatException ne)
		{
			System.out.println("Fail to convert");
		}
		
		// converts the wrapped object into corresponding primitive type.
		
		int x = i.intValue();
		int y = i2.intValue();
		System.out.println("x: " + x + " y: " + y);
		
		// Converts string to primitive, if fail throws NumberFormatException
		int z = Integer.parseInt("34");
		int l = Integer.parseInt("1010101",2);
		System.out.println("z: " + z + " l: " + l);
		
		int m = Integer.parseInt("0123",4);
		int n = Integer.parseInt("abc123",16);
		System.out.println("m: " + m + " n: " + n);
		
		// converts into wrapped object of type that invoke method, else throws NumberFormatException
		Integer a = Integer.valueOf(2); // converts int to Integer
		Integer b = Integer.valueOf("3");
		Integer c = Integer.valueOf("1010101",2);
		System.out.println("a: " + a + " b: " + b + " c: " + c);
		
		// inherit from Object class, return string with value of primitive wrapped in object.
		String s = i.toString();
		System.out.println("s: " + s);
		
		// overloaded static method that takes primitive numeric of that type and return string 
		String k = Integer.toString(12);
		System.out.println("k: " + k);
	} 

}
